<?php
/**
 * WTS THEME
 */

add_action('after_setup_theme', 'wts_setup');

// ** WTS MODS START **
function wts_setup() {
	global $wts;
	$wts['gallery_link'] = 'gallery';
	//$wts['events'] = 'events';
	//$wts['event-list'] = 'event-list';

	$labels = array(
		'name' => 'Gallery',
		'singular_name' => 'Gallery',
		'add_new' => 'Add New',
		'add_new_item' => 'Add New Gallery',
		'edit_item' => 'Edit Gallery',
		'new_item' => 'New Gallery',
		'all_items' => 'All Gallery',
		'view_item' => 'View Gallery',
		'search_items' => 'Search Galleries',
		'not_found' =>  'No galleries found',
		'not_found_in_trash' => 'No galleries found in Trash',
		'parent_item_colon' => '',
		'menu_name' => 'Gallery'

	);
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true, 
		'show_in_menu' => true, 
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'has_archive' => true, 
		'hierarchical' => false,
		'menu_position' => 7,
		'supports' => array('title', 'editor', 'thumbnail', 'comments', 'custom-fields', 'revisions', 'post-formats'),
		'rewrite' => array('slug' => 'gallery')
	);
	
	// to display _parent_id on the post and gallery admin pages.
	add_action('add_meta_boxes', 'wts_custom_meta_boxes');
	// to save the _parent_id correctly when submitted from the post and gallery admin pages.
	add_action('save_post', 'wts_save_postdata', 1, 2);
	// cdn JS files
	add_action('wp_enqueue_scripts', 'wts_cdn_scripts');
	
	// adding query variables to the wordpress as a recognizable custom query variable.
	// why it doesn't do this automatically is beyond me, but whatthehay... gotta follow conventions.
	add_rewrite_tag('%event_view%','([^&]+)');
	add_rewrite_tag('%event_subview%','([^&]+)');
	
	// adding rewrite rules so that wordpress directs the content to the correct pages where the variables can be read and processed
	// accordingly.
	
	// these go to event-list.php
	add_rewrite_rule('^event-list([^/]*)/page/(\d+)','index.php?pagename=event-list&event_view=$matches[1]&paged=$matches[2]','top');
	add_rewrite_rule('^event-list/([^/]*)/([^/]*)','index.php?pagename=event-list&event_view=$matches[1]&event_subview=$matches[2]','top');
	add_rewrite_rule('^event-list/([^/]*)','index.php?pagename=event-list&event_view=$matches[1]','top');
	
	if (function_exists('add_theme_support')) {
		add_theme_support('post-thumbnails');
	}

	if (function_exists('add_image_size')) {
		add_image_size('single', 610, 610, true); 
		add_image_size('event-thumb', 610, 300, true);
		add_image_size('small-thumb', 290, 120, true);
		add_image_size('bb-item', 140, 85, true);
		add_image_size('bb-panel', 473, 281, true);
	}
	
	
	//cleaning up unneeded options
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wp_generator');
}


if (! function_exists('twentyten_comment')) :
/**
 * Template for comments and pingbacks.
 *
 * To override this walker in a child theme without modifying the comments template
 * simply create your own twentyten_comment(), and that function will be used instead.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 *
 * @since Twenty Ten 1.0
 */
function twentyten_comment($comment, $args, $depth) {
	$GLOBALS['comment'] = $comment;
	switch ($comment->comment_type) :
		case '' :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
		<div class="comment-author vcard">
			<?php echo get_avatar($comment, 40); ?>
			<?php printf(__('%s <span class="says">says:</span>', 'twentyten'), sprintf('<cite class="fn">%s</cite>', get_comment_author_link())); ?>
		</div><!-- .comment-author .vcard -->
		<?php if ($comment->comment_approved == '0') : ?>
			<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.', 'twentyten'); ?></em>
			<br />
		<?php endif; ?>

		<div class="comment-meta commentmetadata"><a href="<?php echo esc_url(get_comment_link($comment->comment_ID)); ?>">
			<?php
				/* translators: 1: date, 2: time */
				printf(__('%1$s at %2$s', 'twentyten'), get_comment_date(),  get_comment_time()); ?></a><?php edit_comment_link(__('(Edit)', 'twentyten'), ' ');
			?>
		</div><!-- .comment-meta .commentmetadata -->

		<div class="comment-body"><?php comment_text(); ?></div>

		<div class="reply">
			<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
		</div><!-- .reply -->
	</div><!-- #comment-##  -->

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e('Pingback:', 'twentyten'); ?> <?php comment_author_link(); ?><?php edit_comment_link(__('(Edit)', 'twentyten'), ' '); ?></p>
	<?php
			break;
	endswitch;
}
endif;

/**
 * Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
 *
 * To override twentyten_widgets_init() in a child theme, remove the action hook and add your own
 * function tied to the init hook.
 *
 * @since Twenty Ten 1.0
 * @uses register_sidebar
 */
function twentyten_widgets_init() {
	// Area 1, located at the top of the sidebar.
	register_sidebar(array(
		'name' => __('Primary Widget Area', 'twentyten'),
		'id' => 'primary-widget-area',
		'description' => __('The primary widget area', 'twentyten'),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));

	// Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
	register_sidebar(array(
		'name' => __('Secondary Widget Area', 'twentyten'),
		'id' => 'secondary-widget-area',
		'description' => __('The secondary widget area', 'twentyten'),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));

	// Area 3, located in the footer. Empty by default.
	register_sidebar(array(
		'name' => __('First Footer Widget Area', 'twentyten'),
		'id' => 'first-footer-widget-area',
		'description' => __('The first footer widget area', 'twentyten'),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));

	// Area 4, located in the footer. Empty by default.
	register_sidebar(array(
		'name' => __('Second Footer Widget Area', 'twentyten'),
		'id' => 'second-footer-widget-area',
		'description' => __('The second footer widget area', 'twentyten'),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));

	// Area 5, located in the footer. Empty by default.
	register_sidebar(array(
		'name' => __('Third Footer Widget Area', 'twentyten'),
		'id' => 'third-footer-widget-area',
		'description' => __('The third footer widget area', 'twentyten'),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));

	// Area 6, located in the footer. Empty by default.
	register_sidebar(array(
		'name' => __('Fourth Footer Widget Area', 'twentyten'),
		'id' => 'fourth-footer-widget-area',
		'description' => __('The fourth footer widget area', 'twentyten'),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));
}
/** Register sidebars by running twentyten_widgets_init() on the widgets_init hook. */
add_action('widgets_init', 'twentyten_widgets_init');

/**
 * Removes the default styles that are packaged with the Recent Comments widget.
 *
 * To override this in a child theme, remove the filter and optionally add your own
 * function tied to the widgets_init action hook.
 *
 * This function uses a filter (show_recent_comments_widget_style) new in WordPress 3.1
 * to remove the default style. Using Twenty Ten 1.2 in WordPress 3.0 will show the styles,
 * but they won't have any effect on the widget in default Twenty Ten styling.
 *
 * @since Twenty Ten 1.0
 */
function twentyten_remove_recent_comments_style() {
	add_filter('show_recent_comments_widget_style', '__return_false');
}
add_action('widgets_init', 'twentyten_remove_recent_comments_style');



// *** WTS CODE STARTS ***
function wts_post_meta() {
	// Retrieves tag list of current post, separated by commas.
	$tag_list = get_the_tag_list('', ', ');
	
	echo "<b>Tags:</b> " . $tag_list . '<br />
		<b>Categories:</b> ' . get_the_category_list(', ');
}

function wts_custom_meta_boxes() {
	add_meta_box('wts_parent_id', 'Parent Id', 'wts_parentid_field', 'post', 'side', 'high');
}

function wts_parentid_field() {
	global $post; 
  
	// Noncename needed to verify where the data originated 
	wp_nonce_field(plugin_basename(__FILE__), 'wts_noncename');
  
	// Get the location data if its already been entered 
	$parent_id = get_post_meta($post->ID, '_parent_id', true);

	// Echo out the field 
	echo '<p>Parent Id</p>';
	echo '<input type="text" name="_parent_id" value="' . $parent_id  . '" class="widefat" />';
}

function wts_save_postdata($post_id, $post) {
	// verify this came from the our screen and with proper authorization, 
	// because save_post can be triggered at other times 
	if ( !wp_verify_nonce( $_POST['wts_noncename'], plugin_basename(__FILE__) ))
		return $post->ID;  
	// Is the user allowed to edit the post or page? 
	if ( !current_user_can( 'edit_post', $post->ID )) 
		return $post->ID; 
  
	// OK, we're authenticated: we need to find and save the data 
	// We'll put it into an array to make it easier to loop though. 
  
	if ($_POST['_parent_id'] != '') {
		if(get_post_meta($post->ID, '_parent_id', FALSE)) { // If the custom field already has a value 
			update_post_meta($post->ID, '_parent_id', $_POST['_parent_id']); 
		} else { // If the custom field doesn't have a value 
			add_post_meta($post->ID, '_parent_id', $_POST['_parent_id']);
		}
	} else {
		delete_post_meta($post->ID, '_parent_id');		
	}
}

/*
 * Function that returns the absolute URI for the $link parameter.
 * @param $link
 * @return String URI
 */
function wts_get_link($link) {
	global $wts;
	switch ($link) {
		//case 'event_previews': return bloginfo('wpurl') . '/' . $wts['event-list'] . '/previews';
		//case 'event_reviews': return bloginfo('wpurl') . '/' . $wts['event-list'] . '/reviews';
		//case 'event_gallery': return bloginfo('wpurl') . '/' . $wts['event-list'] . '/gallery';
		
		case 'articles': return bloginfo('wpurl') . '/category/articles';
		case 'news': return bloginfo('wpurl') . '/category/news';
		case 'albums': return bloginfo('wpurl') . '/category/articles/albums';
		case 'gear-reviews': return bloginfo('wpurl') . '/category/articles/gear-reviews';
		case 'general': return bloginfo('wpurl') . '/category/articles/general';
		case 'guest-columns': return bloginfo('wpurl') . '/category/articles/guest-columns';
		case 'interviews': return bloginfo('wpurl') . '/category/articles/interviews';
		
		case 'gallery':  return bloginfo('wpurl') . '/category/gallery';
		case 'interviews-gallery': return wts_get_link('gallery') . '/interviews-gallery';
		case 'events-gallery': return wts_get_link('gallery') . '/events-gallery';
		
		default: return '';
	}
}

function wts_header_events($at = '', $event_id = '')
{
	$status = array('gallery' => 'off',
					'review' => 'off',
					'preview' => 'off',
					'calendar' => 'off',
					);
	if (array_key_exists($at, $status)) {
		$status[$at] = 'on';
	}
	?>
		<div class='tribe-events-calendar-buttons content_box_full'>
			<a class='tribe-events-button-<?php echo $status['preview']; ?>' href='<?php echo wts_get_link('event_previews'); ?>'>Events List</a>
			<a class='tribe-events-button-<?php echo $status['review']; ?>' href='<?php echo wts_get_link('event_reviews'); ?>'>Reviews</a>
			<a class='tribe-events-button-<?php echo $status['gallery']; ?>' href='<?php echo wts_get_link('event_gallery'); ?>'>Photo Gallery</a>
			<a class='tribe-events-button-<?php echo $status['calendar']; ?>' href='<?php echo tribe_get_gridview_link(); ?>'><?php _e('Calendar', 'tribe-events-calendar')?></a>
			<?php
		//}
		?>
		</div>
	<?php
	
}

function wts_header_articles($at = '')
{
	$status = array('articles' => 'off',
					'albums' => 'off',
					'gear-reviews' => 'off',
					'general' => 'off',
					'guest-columns' => 'off',
					'interviews' => 'off'
					);
	if (array_key_exists($at, $status)) {
		$status[$at] = 'on';
	}
	?>
		<div class='tribe-events-calendar-buttons content_box_full'>
			<a class='tribe-events-button-<?php echo $status['articles']; ?>' href='<?php echo wts_get_link('articles'); ?>'>Articles</a>-[
			<a class='tribe-events-button-<?php echo $status['interviews']; ?>' href='<?php echo wts_get_link('interviews'); ?>'>Interviews</a>
			<a class='tribe-events-button-<?php echo $status['albums']; ?>' href='<?php echo wts_get_link('albums'); ?>'>Albums</a>
			<a class='tribe-events-button-<?php echo $status['gear-reviews']; ?>' href='<?php echo wts_get_link('gear-reviews'); ?>'>Gear</a>
			<a class='tribe-events-button-<?php echo $status['guest-columns']; ?>' href='<?php echo wts_get_link('guest-columns'); ?>'>Guest Columns</a>
			<a class='tribe-events-button-<?php echo $status['general']; ?>' href='<?php echo wts_get_link('general'); ?>'>General</a>]
			<?php
		//}
		?>
		</div>
	<?php	
}

function wts_header_gallery()
{
	$status = array('gallery' => 'off',
					'interviews-gallery' => 'off',
					'events-gallery' => 'off'
					);
	if (array_key_exists($at, $status)) {
		$status[$at] = 'on';
	}
	?>
		<div class='tribe-events-calendar-buttons content_box_full'>
			<a class='tribe-events-button-<?php echo $status['gallery']; ?>' href='<?php echo wts_get_link('gallery'); ?>'>Gallery</a>-[
			<a class='tribe-events-button-<?php echo $status['interviews-gallery']; ?>' href='<?php echo wts_get_link('interviews-gallery'); ?>'>Interviews</a>
			<a class='tribe-events-button-<?php echo $status['events-gallery']; ?>' href='<?php echo wts_get_link('events-gallery'); ?>'>Events</a>]
			<?php
		//}
		?>
		</div>
	<?php	
}

/**
 * Function from http://codex.wordpress.org/Function_Reference/get_the_excerpt
 */
function get_the_excerpt_max_charlength($excerpt, $charlength) {
	$charlength++;
	$retval = ''; 
	if ( mb_strlen( $excerpt ) > $charlength ) {
		$subex = mb_substr( $excerpt, 0, $charlength - 5 );
		$exwords = explode( ' ', $subex );
		$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
		if ( $excut < 0 ) {
			$retval .= mb_substr( $subex, 0, $excut );
		} else {
			$retval .= $subex;
		}
		$retval .= '[...]';
	} else {
		$retval .= $excerpt;
	}
	return $retval;
}

/**
 * Get major JS libraries from Google CDN.
 */
function wts_cdn_scripts() {
	wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js');
    wp_enqueue_script( 'jquery' );
	wp_deregister_script( 'jquery-ui' );
    wp_register_script( 'jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js');
    wp_enqueue_script( 'jquery-ui' );
	wp_deregister_script( 'prototype' );
    wp_register_script( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js');
    wp_enqueue_script( 'prototype' );
}
// *** WTS CODE ENDS ***